//
//  BSManager.m
//  PhotographyStudi
//
//  Created by 叶一枫 on 2017/6/5.
//  Copyright © 2017年 叶一枫. All rights reserved.
//

#import "BSManager.h"

@implementation BSManager

+(BSManager *)defaulBSManager{
    
    static BSManager *defaulManager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defaulManager = [[BSManager alloc]init];
    });
    return defaulManager;
}

-(NSString *)fullPath:(NSString *)path directory:(MyFilePath)directory{
    
    NSString *homePath = path;
    
    switch (directory) {
        case 0:{
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            homePath = [paths objectAtIndex:0];
        }
            break;
        case 1:{
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
            homePath = [paths objectAtIndex:0];
        }
            break;
        case 2:{
            NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            homePath = [cacPath objectAtIndex:0];
        }
            break;
        case 3:{
            NSArray *preference = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES);
            homePath = [preference objectAtIndex:0];
        }
            break;
        case 4:
            homePath = NSTemporaryDirectory();;
            break;
        case 5:
            homePath = NSHomeDirectory();
            break;
        case 6:
            homePath = path;
            break;
        default:
            break;
    }
    NSString*full_Path = directory == 6?homePath:[homePath stringByAppendingPathComponent:path];
    return full_Path;
}

-(void)creatDirectory:(NSString *)filePath directory:(MyFilePath)directory{
    
    //想要创建的完整路径
    NSString *fullPath =[self fullPath:filePath directory:directory];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:fullPath isDirectory:&isDir];
    
    if(!(isDirExist && isDir)){
        
        BOOL bCreateDir = [fileManager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            [MBProgressHUD showError:@"创建文件夹失败"];
        }
    }
}


-(void)creatFile:(NSString *)filePath directory:(MyFilePath)directory{

    //想要创建的完整路径
    NSString *fullPath =[self fullPath:filePath directory:directory];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:fullPath isDirectory:&isDir];
    
    if(!(isDirExist && isDir)){
        
        BOOL bCreateDir = [fileManager createFileAtPath:fullPath contents:nil attributes:nil];
        if(!bCreateDir){
            [MBProgressHUD showError:@"创建文件夹失败"];
        }
    }
}


-(void)removeFile:(NSString*)filePath directory:(MyFilePath)directory{

    //想要创建的完整路径
    NSString *fullPath =[self fullPath:filePath directory:directory];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    [fileManager removeItemAtPath:fullPath error:nil];

}

-(FileModel *)fileModel:(NSString *)filePath directory:(MyFilePath)directory{

    NSString *fullPath =[self fullPath:filePath directory:directory];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSDictionary *fileAttrbute = [fileManager attributesOfItemAtPath:fullPath error:nil];

    FileModel*fileM = [[FileModel alloc]init];
    fileM.fileSize = [fileAttrbute objectForKey:@"NSFileSize"];
    fileM.creatTime = [fileAttrbute objectForKey:@"NSFileCreationDate"];
    fileM.fileName = [fileAttrbute objectForKey:@"NSFileGroupOwnerAccountName"];
    return fileM;
}

-(NSArray *)getFiles:(NSString *)filePath directory:(MyFilePath)directory onlyDirectory:(BOOL)onlyDirectory{

    NSString *fullPath =[self fullPath:filePath directory:directory];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL isDir = FALSE;
    BOOL isDirExist = [fileManager fileExistsAtPath:fullPath isDirectory:&isDir];
    
    if(!(isDirExist && isDir)){
        
        BOOL bCreateDir = [fileManager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            [MBProgressHUD showError:@"创建文件夹失败"];
        }
    }
    
    NSMutableArray *files = nil;
    
    NSMutableArray *fileDs = [NSMutableArray array];

    files = [NSMutableArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:fullPath error:nil]];
    
    if ([files containsObject:@".DS_Store"]) {
        [files removeObject:@".DS_Store"];
    }
    
    for (NSString *fileName in files) {
        NSString *namePath =[self fullPath:fileName directory:directory];
        BOOL isDir = FALSE;
       [fileManager fileExistsAtPath:namePath isDirectory:&isDir];
       
        if (isDir) {
            [fileDs addObject:fileName];
        }
    }
    
    if (onlyDirectory) {
        return fileDs;
    }else{
        return files;
    }
}


-(NSString *)getPlaceholderImageNameWithFiletype:(NSString *)fileType{
    
    NSString *placeholderImage = @"defaultType.png";
    
    if ([fileType isEqualToString:@"txt"]) {
        placeholderImage = @"txt.png";
    }else if ([fileType isEqualToString:@"doc"]||[fileType isEqualToString:@"docx"]) {
        placeholderImage = @"word.png";
        
    }else if ([fileType isEqualToString:@"ppt"]||[fileType isEqualToString:@"pptx"]) {
        placeholderImage = @"ppt.png";
        
    }else if ([fileType isEqualToString:@"xlsx"]||[fileType isEqualToString:@"xls"]) {
        placeholderImage = @"excel.png";
        
    }else if ([fileType isEqualToString:@"pdf"]) {
        placeholderImage = @"pdf.png";
        
    }else if([fileType isEqualToString:@"png"]||[fileType isEqualToString:@"jpg"]){
        placeholderImage = @"defaulImage.png";
        
    }else if([fileType isEqualToString:@"rar"]||[fileType isEqualToString:@"jar"]||[fileType isEqualToString:@"zip"]){
        
        placeholderImage = @"zip.png";
    }else if([fileType isEqualToString:@"fab"]||[fileType isEqualToString:@"cad"]){
        
        placeholderImage = @"cad.png";
        
    }else if([fileType isEqualToString:@"csv"]){
        
        placeholderImage = @"csv.png";
    }else if([fileType isEqualToString:@"oft"]||[fileType isEqualToString:@"msg"]){
        
        placeholderImage = @"mail.png";
    }else if([fileType isEqualToString:@"html"]){
        
        placeholderImage = @"html.png";
    }else if([fileType isEqualToString:@"rtf"]){
        
        placeholderImage = @"rtf.png";
    }else if([fileType isEqualToString:@"xmind"]){
        placeholderImage = @"xmind.png";
    }else if([fileType isEqualToString:@"exe"]){
        
        placeholderImage = @"exe.png";
    }else if([fileType isEqualToString:@"numbers"]){
        
        placeholderImage = @"numbers.png";
    }else if([fileType isEqualToString:@"pages"]){
        
        placeholderImage = @"pages.png";
    }else if([fileType isEqualToString:@"keynote"]){
        
        placeholderImage = @"keynote.png";
    }
    
    return placeholderImage;
}

@end
